<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="/include/commonHeader::commonHeader('每日任务')"></head>
<body class="gray-bg layer-blue">
<script src="/js/jquery-ui.min.js"></script>
<div class="wrapper wrapper-content animated fadeInRight full-container full-height" id="summary-page">
    <div class="row">
        <div class="col-xs-8 same-height-box">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="btn-group table-tool" id="table1-toolbar" role="group">
                        <i class="fa fa-arrow-left" onclick="changeDataByDate(-1,'#flightDate')"
                           style="position:absolute;left:0;top:0;padding:8px 6px;color:#1c84c6;cursor:pointer;"></i>
                        <input type="text" placeholder="请选择日期" style="width:120px;"
                               class="table-tool-btn pull-left text-center" id="flightDate"/>
                        <i class="fa fa-arrow-right" onclick="changeDataByDate(1,'#flightDate')"
                           style="position:absolute;left:98px;top:0;padding:8px 6px;color:#1c84c6;cursor:pointer;"></i>
                        <button type="button" class="btn btn-outline btn-success btn-refresh" data-toggle="tooltip"
                                title="刷新" id="btn-refresh"><i class="fa fa-refresh" aria-hidden="true"></i>
                            <!--<i class="fa fa-search" aria-hidden="true"></i>-->
                        </button>
                    </div>
                    <div class="btn-group table-tool pull-right" role="group">
                        <button type="button" class="btn btn-outline btn-success btn-refresh" data-toggle="tooltip"
                                title="添加" id="btn-add"><i class="fa fa-plus" aria-hidden="true"></i>
                        </button>
                        <button type="button" class="btn btn-outline btn-success btn-refresh" data-toggle="tooltip"
                                title="合并" id="btn-merge"><i class="fa fa-link" aria-hidden="true"></i>
                        </button>
                        <button type="button" class="btn btn-outline btn-success btn-refresh" data-toggle="tooltip"
                                title="删除" id="btn-del"><i class="fa fa-trash" aria-hidden="true"></i>
                        </button>
                        <button type="button" class="btn btn-outline btn-success btn-refresh" data-toggle="tooltip"
                                title="保存" id="btn-save"><i class="fa fa-save" aria-hidden="true"></i>
                            <!--<i class="fa fa-search" aria-hidden="true"></i>-->
                        </button>
                    </div>
                    <div id="calendar">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-4 same-height-box">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="btn-group table-tool" role="group">
                                <h4>已组航班</h4>
                            </div>
                            <div class="btn-group table-tool pull-right" role="group">
                                <div class="checkSwitch hide" id="internationalTimeZoneMore6" >
                                    <input class="checkSwitch-checkbox" id="onoffswitch" type="checkbox">
                                    <label class="checkSwitch-label" for="onoffswitch">
                                        <span class="checkSwitch-inner" data-on="是" data-off="否"></span>
                                        <span class="checkSwitch-switch">国际</br>时差</span>
                                    </label>
                                </div>
                                <div  class=" pull-left hide" id="minMaleDiv">
                                    <span>最少男性</span>
                                    <input type="text"  id="minMale" class="table-tool-btn"
                                           onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                                           style="width:70px;">
                                </div>
                                <div class="checkSwitch hide" id="isBadRouteDiv" >
                                    <input class="checkSwitch-checkbox" id="onoffswitchBadRoute" type="checkbox">
                                    <label class="checkSwitch-label" for="onoffswitchBadRoute">
                                        <span class="checkSwitch-inner" data-on="是" data-off="否"></span>
                                        <span class="checkSwitch-switch">正常</br>航线</span>
                                    </label>
                                </div>
                                <!--<span id="isBadRouteDiv" class="hide">
                                    <select name="" id="isBadRoute">
                                        <option value="0">正常航线</option>
                                        <option value="1">差航线</option>
                                    </select>
                                </span>-->
                                <button type="button" class="btn btn-outline btn-success" data-toggle="tooltip"
                                        title="删除" id="btn-del-task"><i class="fa fa-trash" aria-hidden="true"></i>
                                </button>
                            </div>
                            <table id="table-task-done" data-classes="table table-hover table-condensed">
                                <thead>
                                <tr>
                                    <th data-field="flightDate" data-formatter="flightDateFormatter">日期</th>
                                    <th data-field="flightNo">航班</th>
                                    <th data-field="_departureAirport">发站</th>
                                    <th data-field="_arrivalAirport">到站</th>
                                    <th data-field="std" data-formatter="timeFormatter">始时</th>
                                    <th data-field="sta" data-formatter="timeFormatter">止时</th>
                                    <th data-field="acType">机型</th>
                                    <th data-field="acReg">机号</th>
                                    <th data-field="serial">机序</th>
                                    <th data-field="operation" data-formatter="delFromTaskLoopFormatter" data-events="delFromTaskLoop">操作</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-xs-12">
                            <h4>未组航班</h4>
                            <ul class="date-tabs" id="date-tabs">
                            </ul>
                            <table id="table-task-not" data-classes="table table-hover table-condensed">
                                <thead>
                                <tr>
                                    <th data-field="flightNo">航班</th>
                                    <th data-field="_departureAirport">发站</th>
                                    <th data-field="_arrivalAirport">到站</th>
                                    <th data-field="std" data-formatter="timeFormatter">始时</th>
                                    <th data-field="sta" data-formatter="timeFormatter">止时</th>
                                    <th data-field="acType">机型</th>
                                    <th data-field="acReg">机号</th>
                                    <th data-field="serial">机序</th>
                                    <th data-field="operation" data-formatter="addToTaskLoopFormatter" data-events="addToTaskLoop">操作</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<ul class="dropdown-menu right-menu" id="rightMenu">
    <li class="prevent">
        <a href="javascript:void(0)" onclick="createNewTaskLoopFact()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> 分离任务环
        </a>
    </li>
    <li class="prevent arrivalAirportRestTime">
        <a href="javascript:void(0)" onclick="editArrivalAirportRestTime()">
            <span class="glyphicon glyphicon-time" aria-hidden="true"></span> 过站休息期
        </a>
    </li>
</ul>
<div class="wrapper wrapper-content layer-body" id="layerBody" style="display:none;">
    <form class="form-horizontal">
        <div class="row person-item">
            <div class="col-sm-10 col-sm-offset-1">
                <div class="form-group">
                    <div class="col-xs-4 form-label-v">
                        <label>过站休息期:</label>
                    </div>
                    <div class="text-left col-sm-8">
                        <input class="form-control number" type="number" id="timer" />
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<script src='/plugins/fullcalendar/fullcalendar.min.js'></script>
<script src='/plugins/fullcalendar/scheduler.min.js'></script>
<script src='/plugins/fullcalendar/locale-all.js'></script>
<script  th:inline="javascript">
    var dragEvent = null;
    var currentFlightLine = {};//当前点击的航线信息
    var currentTabDay = null;
    var index;//loading 图示
    var contextmenuResourceId; //右键分离任务环 crewLinkLineNo
    var contextmenuFlightId; //右键航班id
    var contextmenuFlightRestTime = 0; //右键航班过站休息期
    var dutyCode =  [[${dutyCode}]];//页面是否只能查看
    toastr.options = {
        "timeOut": "500",
    }
    $(document).ready(function () {
        $.fn.select2.defaults.set("theme", "bootstrap"); //使用select2-bootstrap主题
        //控件初始化
        $("#flightDate").datetimepicker({
            minView: "month",
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            autoclose: true,
            todayBtn: true,
            bootcssVer:3,
            todayHighlight: true,
            pickerPosition: "bottom-right"
        }).on("changeDate",function(){
            $("#table-flight-not").bootstrapTable("refresh");
            $("#table-flight-done").bootstrapTable("refresh");
            $("#table-crew").bootstrapTable("refresh");
            currentFlightLine = {};
            $("#isBadRouteDiv").addClass("hide");
            $("#internationalTimeZoneMore6").addClass("hide");
            $("#minMaleDiv").addClass("hide");
            $('#calendar').fullCalendar("refetchResources");
            $('#calendar').fullCalendar("gotoDate",$("#flightDate").val());
        });
        $("#flightDate").datetimepicker('setDate', new Date()).trigger('change');
        currentTabDay = $("#flightDate").val();
        init();
        $("#table-task-done").bootstrapTable({
            url: "/pilotLinkLine/listFlightBySameTaskLoopFact",
            clickToSelect: true,
            singleSelect: true,
            showRefresh: false,                  //是否显示刷新按钮
            striped: false,//行间隔色
            locale: "zh",
            rowStyle: function(row,index) {
                var classes = '';
                if(moment(new Date(row.flightDate)).format("MM-DD") == moment(new Date($("#flightDate").val())).format("MM-DD")){
                    classes = 'info';
                }
                return {classes:classes};
            },
            queryParams: function (params) {
                var result = {
                    flightDate: $("#flightDate").val(),
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine?currentFlightLine.resourceId:''
                };
                return result;
            },
            onClickRow: function (row, $element) {
               /* $element.addClass('info').siblings().removeClass('info').end();*/
            },
            onLoadSuccess: function (data) {
            },
        });
        $("#table-task-not").bootstrapTable({
            url: "/pilotLinkLine/listFlightNotHaveTaskLoopFact",
            clickToSelect: true,
            singleSelect: true,
            showRefresh: false,                  //是否显示刷新按钮
            striped: true,//行间隔色
            locale: "zh",
            height: '300',
            queryParams: function (params) {
                var result = {
                    flightDateStart: currentTabDay,
                    dutyCode:dutyCode,
                    flightDateEnd:currentTabDay
                };
                return result;
            },
            onClickRow: function (row, $element) {
                $element.addClass('info').siblings().removeClass('info').end();
            },
            onLoadSuccess: function (data) {
                $("#table-task-not").bootstrapTable("resetView");
            },
        });

        /*$("#isBadRoute").select2({
            width:80,
            minimumResultsForSearch: Infinity,
        }).on("change.select2",function(){
            var isBadRoute = $("#isBadRoute").val() == 0?false:true;
            $.ajax({
                type: "post",
                url: "/pilotLinkLine/badRoute",
                data: {
                    flightDate: $("#flightDate").val(),
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine.resourceId,
                    badRoute:isBadRoute
                },
                dataType: 'json',
                success: function (data) {
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success(data.message);
                        //刷新日历及已组航班和未组航班数据
                    }else{
                        toastr.error(data.message);
                    }
                },
                error:function(){

                }
            });
        });*/
        $("#onoffswitch").on('click', function(){
            $.ajax({
                type: "post",
                url: "/pilotLinkLine/internationalTimeZoneMore6",
                data: {
                    flightDate: $("#flightDate").val(),
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine.resourceId,
                    internationalTimeZoneMore6:$("#onoffswitch").is(':checked')
                },
                dataType: 'json',
                success: function (data) {
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success(data.message);
                        //刷新日历及已组航班和未组航班数据

                    }else{
                        toastr.error(data.message);
                    }
                },
                error:function(){

                }
            });
        });
        $("#onoffswitchBadRoute").on('click', function(){
            $.ajax({
                type: "post",
                url: "/pilotLinkLine/badRoute",
                data: {
                    flightDate: $("#flightDate").val(),
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine.resourceId,
                    badRoute:$("#onoffswitchBadRoute").is(':checked')
                },
                dataType: 'json',
                success: function (data) {
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success(data.message);
                        //刷新日历及已组航班和未组航班数据

                    }else{
                        toastr.error(data.message);
                    }
                },
                error:function(){

                }
            });
        });
        $("#minMale").on("blur",function(){
            $.ajax({
                type: "post",
                url: "/pilotLinkLine/minMale",
                data: {
                    flightDate: $("#flightDate").val(),
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine.resourceId,
                    minMale:$("#minMale").val()
                },
                dataType: 'json',
                success: function (data) {
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success(data.message);
                        //刷新日历及已组航班和未组航班数据
                    }else{
                        toastr.error(data.message);
                    }
                },
                error:function(){

                }
            });
        });
        //绑定查询按钮
        $("#btn-refresh").click(function () {
            $("#calendar").fullCalendar("refetchResources");
            $('#calendar').fullCalendar("gotoDate",$("#flightDate").val());
            currentFlightLine = {};
            $("#isBadRouteDiv").addClass("hide");
            $("#internationalTimeZoneMore6").addClass("hide");
            $("#minMaleDiv").addClass("hide");
            $("#table-task-done").bootstrapTable("refresh");
            constructDateTabs($("#flightDate").val());
            $(".date-tab.active").trigger("click");
        });
        //合并连线
        $("#btn-merge").click(function(){
            var crewLinkLineNo;
            var crewLinkLineNoMoveTo;
            var checkArr = [];
            $('.merge-check').each(function(){
                if($(this).is(':checked')){
                    checkArr.push($(this).data("resource-id"));
                }
            });
            if(checkArr.length == 2){
                crewLinkLineNo = checkArr[0];
                crewLinkLineNoMoveTo = checkArr[1];
            }else{
                if(!$("#toast-container")[0]){
                    toastr.warning("请选择两条连线合并！");
                }
                return false;
            }
            $.ajax({
                type: "post",
                url: "/pilotLinkLine/moveTaskLoop",
                data: {
                    flightDate: $("#flightDate").val(),
                    crewLinkLineNo:crewLinkLineNo,
                    dutyCode:dutyCode,
                    crewLinkLineNoMoveTo:crewLinkLineNoMoveTo
                },
                dataType: 'json',
                success: function (data) {
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success(data.message);
                        //刷新日历及已组航班和未组航班数据
                        $("#table-task-not").bootstrapTable("refresh");
                        $("#table-task-done").bootstrapTable("refresh");
                        $("#calendar").fullCalendar("refetchEvents");
                        $("#calendar").fullCalendar("refetchResources");
                    }else{
                        toastr.error(data.message);
                    }
                },
                error:function(){

                }
            });
        });
        $("#btn-add").click(function () {
            addResource();
        });
        $("#btn-del").click(function(){
            if(!currentFlightLine.resourceId){
                if(!$("#toast-container")[0]){
                    toastr.warning("请选择需要删除的连线！");
                }
                return false;
            }
            $.ajax({
                type: "post",
                url: "/pilotLinkLine/deleteLinkLine",
                data: {
                    flightDate: $("#flightDate").val(),
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine.resourceId
                },
                dataType: 'json',
                success: function (data) {
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success(data.message);
                        //刷新日历及已组航班和未组航班数据
                        $("#table-task-not").bootstrapTable("refresh");
                        $("#table-task-done").bootstrapTable("refresh");
                        $("#calendar").fullCalendar("refetchEvents");
                        $("#calendar").fullCalendar("refetchResources");
                    }else{
                        toastr.error(data.message);
                    }
                },
                error:function(){

                }
            });
        });
        $("#btn-del-task").click(function(){
            if(!currentFlightLine.resourceId){
                if(!$("#toast-container")[0]){
                    toastr.warning("请选择需要删除的连线！");
                }
                return false;
            }
            layer.confirm('确认删除该任务环吗?', function(index){
                $.ajax({
                    type: "post",
                    url: "/pilotLinkLine/deleteTaskLoop",
                    data: {
                        flightDate: $("#flightDate").val(),
                        dutyCode:dutyCode,
                        crewLinkLineNo:currentFlightLine.resourceId
                    },
                    dataType: 'json',
                    success: function (data) {
                        if(!data.errorCode && (data.success == true ||data.success == 'true')){
                            toastr.success(data.message);
                            //刷新日历及已组航班和未组航班数据
                            $("#table-task-not").bootstrapTable("refresh");
                            $("#table-task-done").bootstrapTable("refresh");
                            $('#calendar').fullCalendar('removeResource',currentFlightLine.resourceId);
                        }else{
                            toastr.error(data.message);
                        }
                    },
                    error:function(){

                    }
                });
                layer.close(index);
            });
        });

        /*连线生效*/
        $("#btn-save").click(function(){
            var loadingIndex = layer.load();
            $.ajax({
                type: "post",
                url: "/pilotLinkLine/confirm",
                data: {
                    flightDate: $("#flightDate").val(),
                    dutyCode:dutyCode
                },
                dataType: 'json',
                success: function (data) {
                    //事件设置可以拖动的起止时间
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success('分离任务环成功！');
                        layer.close(loadingIndex);
                        //刷新日历及已组航班和未组航班数据
                        $("#table-task-not").bootstrapTable("refresh");
                        $("#table-task-done").bootstrapTable("refresh");
                        $('#calendar').fullCalendar("refetchResources");
                        $('#calendar').fullCalendar("refetchEvents");
                    }else{
                        toastr.options = {
                            "closeButton": true,
                            "timeOut": "600000",
                            "positionClass": "toast-top-full-width",
                        }
                        toastr.error(data.message);
                        layer.close(loadingIndex);
                    }
                },
                error:function(){
                    layer.close(loadingIndex);
                }
            });
        });
        resizeTable();
        $(window).resize(function () {
            resizeTable();
        });
        constructDateTabs($("#flightDate").val());
    });
    //右键菜单隐藏
    $(document).on("mousedown", function (event) {
        //button等于0代表左键，button等于1代表中键
        if ((event.button == 0 || event.button == 1) && (event.target.offsetParent && event.target.offsetParent.tagName != 'UL')) {
            $("#rightMenu").hide();//获取菜单停止动画，进行隐藏使用淡出效果,未使用动画
        }
    });
    function init() {
        $('#calendar').fullCalendar({
            schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives', //scheduler非商业授权证书
            now: $("#flightDate").val(),
            editable: true,
            droppable: true,
            minTime: "05:00:00",
            maxTime: "29:00:00",
            aspectRatio:'1.8',
            height:'parent',
            header: {
                left: '',
                center: '',
                right: ''
            },
            locale: 'zh-cn',
            defaultView: 'timelineDay',
            views: {
                timelineDay: {
                    type: 'timeline',
                    slotDuration: '01:00',
                }
            },
            resourceAreaWidth: '210px',
            eventDurationEditable: false,//不可resize
            resourceColumns: [
                {
                    labelText: '',
                    width: '20px',
                    render: function(resource, el) {
                        return '<input type="checkbox" class="merge-check" data-resource-id="'+resource.id+'"/>'
                    }
                },
                {
                    labelText: '序号',
                    width: '30px',
                    field: 'crewLinkLineAndDorI'
                },
                {
                    labelText: '机型',
                    width: '45px',
                    field: 'acTypeAndAcReg',
                    align:'center'
                },

                {
                    labelText: '飞行时间',
                    field: 'flyAndDutyDuration'
                },

                {
                    labelText: '起止时间',
                    field: 'tdAndTa',
                    text: function(resource, el) {
                        return resource.tdHHmm + '\n' + resource.taHHmm;
                    }
                }
            ],
            resources: function(callback) {
                $.ajax({
                    type: "post",
                    url: "/pilotLinkLine/listResource",
                    data: {
                        flightDate: $("#flightDate").val(),
                        dutyCode:dutyCode
                    },
                    dataType: 'json',
                    async: false,
                    success: function (datas) {
                        callback(datas);
                    }
                });
            },
            events: function (start, end, timezone, callback) {
                $.ajax({
                    url: '/pilotLinkLine/listEvent',
                    dataType: 'json',
                    type: "post",
                    data: {
                        flightDate: $("#flightDate").val(),
                        dutyCode:dutyCode
                    },
                    success: function (datas) {
                        //事件设置可以拖动的起止时间
                        for (var i = 0; i < datas.length; i++) {
                            datas[i].constraint = {
                                start: datas[i].start,
                                end: datas[i].end
                            }
                        }
                        callback(datas);
                    }
                });

            },
            loading:function(isLoading,view){
                if(isLoading){
                    index = layer.load();
                }else{
                    layer.close(index);
                }
            },
            eventRender: function (event, $element) {
                var html = '<div class="text-center top-flightVoyage">'+event.airPortCityName+
                            '</div>'+
                            '<div class="text-center bottom-flightCom">'+event.flightNo;
                            '</div>';
                $element.find(".fc-content .fc-title").attr("data-flight-id",event.id);
                $element.find(".fc-content .fc-title").attr("data-flight-restTime",event.restTime);
                $element.find(".fc-content .fc-title").html(html);
                var iconHtml = '';
                if(event.changeAcFlag){
                    iconHtml += ' <span class="glyphicon glyphicon-plane text-warning"></span>';
                }
                if(event.restTime){
                    iconHtml += ' <span class="glyphicon glyphicon-time text-info" style="transform: scale(0.7);"></span>';
                }

                $element.find(".fc-content").append('<span class="right-end">'+iconHtml+'</span>');

            },
            eventMouseover: function (event, jsEvent, view) {

            },
            eventMouseout: function (event, jsEvent, view) {
            },
            eventDragStart: function (event, jsEvent, ui, view) {
                dragEvent = event;
            },
            eventDrop: function (event, delta, revertFunc, jsEvent, ui, view) {
               $.ajax({
                    url:'/pilotLinkLine/moveFlight',
                    dataType:'json',
                    type:'post',
                    data:{
                        flightDate:event.flightDate,
                        flightId:event.id,
                        crewLinkLineNoOld:dragEvent.resourceId,
                        crewLinkLineNoNew:event.resourceId,
                        dutyCode:dutyCode
                    },
                    success:function(data){
                        if(!data.errorCode && (data.success == true ||data.success == 'true')){
                            toastr.success('调整航班成功！');
                            //刷新日历及已组航班和未组航班数据
                            $("#table-flight-not").bootstrapTable("refresh");
                            $("#table-flight-done").bootstrapTable("refresh");
                        }else{
                            toastr.error(data.message);
                        }
                    },
                    error:function(){

                    }
                })

            },
            drop: function(date, jsEvent, ui, resourceId) {
            },
            eventDragStop:function( event, jsEvent, ui, view ) {
            },
            eventAllow: function (dropInfo, draggedEvent) {
            },
            eventClick: function (event, jsEvent, view) {
                currentFlightLine = event;
            },
            eventAfterAllRender:function(view){
                $(".fc-time-area tbody tr").on("contextmenu", function (event) {
                    contextmenuResourceId = $(this).data("resource-id");
                    contextmenuFlightId = (event.target.offsetParent && $(event.target.offsetParent).hasClass("fc-title"))?$(event.target.offsetParent).data("flight-id"):'';
                    contextmenuFlightRestTime = (event.target.offsetParent && $(event.target.offsetParent).hasClass("fc-title"))?$(event.target.offsetParent).data("flight-resttime"):0;

                    var pageX = event.pageX;//鼠标单击的x坐标
                    var pageY = event.pageY;//鼠标单击的y坐标
                    var $contextMenu = $("#rightMenu");
                    var cssObj = {
                        top: pageY + "px",//设置菜单离页面上边距离，top等效于y坐标
                        left: pageX + "px"//设置菜单离页面左边距离，left等效于x坐标
                    };
                    //判断横竖位置（pageX）+自定义菜单宽度之和，如果超过页面宽度及为溢出，需要特殊处理；
                    var menuWidth = $contextMenu.width();
                    var menuHeight = $contextMenu.height();
                    var pageWidth = $(document).width();
                    var pageHeight = $(document).height();
                    if (pageX + menuWidth * 2 > pageWidth) {
                        cssObj.left = pageX - 100 + "px"; //防止溢出
                    }
                    if (pageY + menuHeight > pageHeight) {
                        cssObj.top = pageY - 100 + "px"; //
                    }
                    //设置菜单的位置
                    if(contextmenuResourceId){
                        $contextMenu.css(cssObj).show();//显示使用淡入效果,比如不需要动画可以使用show()替换;
                    }
                    if(!contextmenuFlightId){
                        $contextMenu.find(".arrivalAirportRestTime").hide();
                    }else{
                        $contextMenu.find(".arrivalAirportRestTime").show();
                    }
                    event.preventDefault();//阻止浏览器与事件相关的默认行为；此处就是弹出右键菜单
                });
                $(".fc-resource-area tbody tr ,.fc-time-area tbody tr").off("click").on("click",function(event){
                    if(event.target.type != "checkbox"){
                        var resourceId = $(this).data("resource-id");
                        $(".fc-resource-area tbody tr[data-resource-id="+resourceId+"] ,.fc-time-area tbody tr[data-resource-id="+resourceId+"]").addClass("active-td").siblings().removeClass("active-td");
                        currentFlightLine.resourceId = resourceId;
                        //$("#table-task-done").bootstrapTable("refresh");
                        $.ajax({
                            url: '/pilotLinkLine/taskLoopInfo',
                            dataType: 'json',
                            type: "post",
                            data: {
                                flightDate:$("#flightDate").val(),
                                dutyCode:dutyCode,
                                crewLinkLineNo:resourceId
                            },
                            success: function (data) {
                                //事件设置可以拖动的起止时间
                                if(!data.errorCode && (data.success == true ||data.success == 'true')){
                                    $("#isBadRouteDiv").removeClass("hide");
                                    $("#internationalTimeZoneMore6").removeClass("hide");
                                    $("#minMaleDiv").removeClass("hide");
                                   // var isBadRoute = data.data.badRoute?1:0;
                                    $("#onoffswitchBadRoute").prop("checked",data.data.badRoute);
                                    $("#onoffswitch").prop("checked",data.data.internationalTimeZoneMore6);
                                    $("#minMale").val(data.data.minMale?data.data.minMale:0);
                                    $("#table-task-done").bootstrapTable("refresh");
                                }else{
                                    toastr.error(data.message);
                                }
                            }
                        });
                    }
                });
            }
        });

    }

    function strIsEmpty(str) {
        if (str == null || str == '' || str == undefined) {
            return true;
        } else {
            return false;
        }
    }

    function timeFormatter(value, row, index) {
        if (!value) {
            return '';
        } else {
            return moment(new Date(value)).format("HH:mm");
        }
    }

    function flightDateFormatter(value, row, index){
        if (!value) {
            return '';
        } else {
            return moment(new Date(value)).format("MM-DD");
        }
    }
    function delFromTaskLoopFormatter(value, row, index){
        return '<a class="del ml10" href="javascript:void(0)" title="删除">' +
            '<span class="glyphicon glyphicon-arrow-down"></span>' +
            '</a>';
    }
    function addToTaskLoopFormatter(value, row, index){
        return '<a class="add ml10" href="javascript:void(0)" title="添加">' +
            '<span class="glyphicon glyphicon-arrow-up"></span>' +
            '</a>';
    }
    window.delFromTaskLoop = {
        'click .del': function(e, value, row, index) {
            //从任务环中删除航班
            if(!currentFlightLine || !currentFlightLine.resourceId){
                if(!$("#toast-container")[0]){
                    toastr.warning("请选择连线！");
                }
                return false;
            }
            $.ajax({
                url: '/pilotLinkLine/removeFlightFromLinkLine',
                dataType: 'json',
                type: "post",
                data: {
                    flightId: row.flightId,
                    flightDate:row.flightDate,
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine.resourceId
                },
                success: function (data) {
                    //事件设置可以拖动的起止时间
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success('拆除航班成功！');
                        //刷新日历及已组航班和未组航班数据
                        $("#table-task-not").bootstrapTable("refresh");
                        $("#table-task-done").bootstrapTable("refresh");
                        $('#calendar').fullCalendar("refetchEvents");
                    }else{
                        toastr.error(data.message);
                    }
                }
            });
        }
    }
    window.addToTaskLoop = {
        'click .add': function(e, value, row, index) {
            //航班加入到任务环
            if(!currentFlightLine || !currentFlightLine.resourceId){
                if(!$("#toast-container")[0]){
                    toastr.warning("请选择连线！");
                }
                return false;
            }
            $.ajax({
                url: '/pilotLinkLine/addFlightToLinkLine',
                dataType: 'json',
                type: "post",
                data: {
                    flightId: row.flightId,
                    flightDate:$("#flightDate").val(),
                    dutyCode:dutyCode,
                    crewLinkLineNo:currentFlightLine.resourceId
                },
                success: function (data) {
                    //事件设置可以拖动的起止时间
                    if(!data.errorCode && (data.success == true ||data.success == 'true')){
                        toastr.success('组合航班成功！');
                        //刷新日历及已组航班和未组航班数据
                        $("#table-task-not").bootstrapTable("refresh");
                        $("#table-task-done").bootstrapTable("refresh");
                        $('#calendar').fullCalendar("refetchResources");
                        $('#calendar').fullCalendar("refetchEvents");
                    }else{
                        toastr.error(data.message);
                    }
                }
            });
        }
    }
    function addResource() {
        $.ajax({
            type: "post",
            url: "/pilotLinkLine/getNewCrewLinkLineNo",
            data: {
                flightDate: $("#flightDate").val(),
                dutyCode:dutyCode
            },
            dataType: 'json',
            success: function (datas) {
                $('#calendar').fullCalendar(
                    'addResource',
                    {
                        acTypeAndAcReg: '',
                        crewLinkLineAndDorI: datas.data,
                        flyAndDutyDuration: '',
                        id: datas.data
                    },
                    true // scroll to the new resource?
                );
                var resourceId = datas.data;
                $(".fc-time-area tbody tr[data-resource-id="+resourceId+"]").on("contextmenu", function (event) {
                    contextmenuResourceId = resourceId;
                    var pageX = event.pageX;//鼠标单击的x坐标
                    var pageY = event.pageY;//鼠标单击的y坐标
                    var $contextMenu = $("#rightMenu");
                    var cssObj = {
                        top: pageY + "px",//设置菜单离页面上边距离，top等效于y坐标
                        left: pageX + "px"//设置菜单离页面左边距离，left等效于x坐标
                    };
                    //判断横竖位置（pageX）+自定义菜单宽度之和，如果超过页面宽度及为溢出，需要特殊处理；
                    var menuWidth = $contextMenu.width();
                    var menuHeight = $contextMenu.height();
                    var pageWidth = $(document).width();
                    var pageHeight = $(document).height();
                    if (pageX + menuWidth * 2 > pageWidth) {
                        cssObj.left = pageX - 100 + "px"; //防止溢出
                    }
                    if (pageY + menuHeight > pageHeight) {
                        cssObj.top = pageY - 100 + "px"; //
                    }
                    //设置菜单的位置
                    if(contextmenuResourceId){
                        $contextMenu.css(cssObj).show();//显示使用淡入效果,比如不需要动画可以使用show()替换;
                        $contextMenu.find(".arrivalAirportRestTime").hide();
                    }
                    event.preventDefault();//阻止浏览器与事件相关的默认行为；此处就是弹出右键菜单
                });
                $(".fc-resource-area tbody tr[data-resource-id="+resourceId+"] ,.fc-time-area tbody tr[data-resource-id="+resourceId+"]").click(function(){
                    $(".fc-resource-area tbody tr[data-resource-id="+resourceId+"] ,.fc-time-area tbody tr[data-resource-id="+resourceId+"]").addClass("active-td").siblings().removeClass("active-td");
                    currentFlightLine.resourceId = resourceId;
                    $("#table-task-done").bootstrapTable("refresh");
                });
            },
            error:function(){

            }
        });
    }

    function resizeTable() {
        $(".table").bootstrapTable("resetView");
    }

    function changeDataByDate(dir,dom){
        changeDate(dir,dom);
        $('#calendar').fullCalendar("refetchResources");
        $('#calendar').fullCalendar("gotoDate",$("#flightDate").val());
        currentFlightLine = {};
        $("#table-task-done").bootstrapTable("refresh");
        $("#isBadRouteDiv").addClass("hide");
        $("#internationalTimeZoneMore6").addClass("hide");
        $("#minMaleDiv").addClass("hide");
        constructDateTabs($("#flightDate").val());
        $(".date-tab.active").trigger("click");
    }
    function constructDateTabs(date){
       var prevDayFull = addDate(date,-1);
       var prevDay = prevDayFull.substr(5);
       var nextDayFull = addDate(date,1);
       var nextDay = nextDayFull.substr(5);
       var activeDay = date.substr(5);
       var html ='<li onclick="changeActiveDay(this)"><a><</a></li>'+
           '<li class="date-tab" data-index="0" data-day="'+prevDayFull+'" onclick="changeActiveDay(this)"><a>'+prevDay+'</a></li>'+
           '<li class="date-tab active" data-index="1" data-day="'+date+'" onclick="changeActiveDay(this)"><a>'+activeDay+'</a></li>'+
           '<li class="date-tab" data-index="2" data-day="'+nextDayFull+'" onclick="changeActiveDay(this)"><a>'+nextDay+'</a></li>'+
           '<li onclick="changeActiveDay(this,1)"><a>></a></li>';
       $("#date-tabs").html(html);
    }
    function changeActiveDay(dom,dir){
        var thisDomLi = $(dom);
        if(thisDomLi.hasClass('date-tab')){
            currentTabDay = thisDomLi.data("day");
            $("#table-task-not").bootstrapTable("refresh");
            thisDomLi.addClass("active").siblings().removeClass("active");
        }else{
            if($("#date-tabs li.active").data("index") == 0){
                //激活元素在第一个
                constructDateTabs($("#date-tabs li.active").data("day"));
            }else if($("#date-tabs li.active").data("index") == 2){
                constructDateTabs($("#date-tabs li.active").data("day"));
            }else{
                //激活元素不在第一个
                if(dir == 1){
                    $("#date-tabs li.active").next().trigger("click");
                }else{
                    $("#date-tabs li.active").prev().trigger("click");
                }
            }
        }
    }

    function dragInit(){
        $('#table-flight-not tbody tr').each(function() {
            var flightId = $(this).data("uniqueid");
            var rowData = $('#table-flight-not').bootstrapTable("getRowByUniqueId", flightId);
            // store data so the calendar knows to render an event upon drop
            $(this).data('event', {
                title: '123', // use the element's text as the event title
                start:rowData.std,
                end:rowData.sta,
                airPortCityName:'长沙青岛',
                flightNo:rowData.flightNo,
                constraint:{
                    start: rowData.std,
                    end: rowData.sta
                },
                stick: true // maintain when user navigates (see docs on the renderEvent method)
            });
            // make the event draggable using jQuery UI
            $(this).draggable({
                zIndex: 999,
                revert: true,      // will cause the event to go back to its
                revertDuration: 0,  //  original position after the drag
                cursorAt: {top: 10, left: 20},
                helper: function (event) {
                    var resultHTML = "";
                    if (flightId) {

                        resultHTML += "<div class='ui-widget-header'>" +
                            "<span class='name'>" + rowData.flightNo + "</span>" +
                            "<span class='workcode'>123</span></div>";
                    }
                    return resultHTML;

                }
            });

        });
    }
    function createNewTaskLoopFact(){
        $("#rightMenu").hide();
        var loadingIndex = layer.load();
        $.ajax({
            type: "post",
            url: "/pilotLinkLine/createNewTaskLoopFact",
            data: {
                flightDate: $("#flightDate").val(),
                dutyCode:dutyCode,
                crewLinkLineNo:contextmenuResourceId
            },
            dataType: 'json',
            success: function (data) {
                //事件设置可以拖动的起止时间
                if(!data.errorCode && (data.success == true ||data.success == 'true')){
                    toastr.success('分离任务环成功！');
                    layer.close(loadingIndex);
                    //刷新日历及已组航班和未组航班数据
                    $("#table-task-not").bootstrapTable("refresh");
                    $("#table-task-done").bootstrapTable("refresh");
                    $('#calendar').fullCalendar("refetchResources");
                    $('#calendar').fullCalendar("refetchEvents");
                }else{
                    toastr.error(data.message);
                    layer.close(loadingIndex);
                }
            },
            error:function(){
                layer.close(loadingIndex);
            }
        });
    }
    function editArrivalAirportRestTime(){
        $("#rightMenu").hide();
        layer.open({
            type: 1,
            skin: 'layui-layer-demo', //样式类名
            area: ['350px', '200px'],
            btn: ["保存", "取消"],
            zIndex: 999,
            title: "修改过站休息期",
            content: $("#layerBody"),
            yes: function (indexLayer, layero) {
                $.ajax({
                    type: "post",
                    url: "/pilotLinkLine/arrivalAirportRestTime",
                    data: {
                        restTime: $("#timer").val(),
                        dutyCode:dutyCode,
                        flightId:contextmenuFlightId
                    },
                    dataType: 'json',
                    success: function (data) {
                        layer.close(indexLayer);
                        //事件设置可以拖动的起止时间
                        if(!data.errorCode && (data.success == true ||data.success == 'true')){
                            toastr.success('修改过站休息期成功！');
                            $('#calendar').fullCalendar("refetchResources");
                            $('#calendar').fullCalendar("refetchEvents");
                        }else{
                            toastr.error(data.message);
                        }
                    },
                    error:function(){
                        layer.close(indexLayer);
                    }
                });
            },
            cancel: function (indexLayer, layero) {
            },
            success: function (indexLayer, index) {
                $("#timer").val(contextmenuFlightRestTime);
            }
        });

    }
</script>
</body>
</html>